import json
from walrus import Database

db = Database(
    host='redis-14976.c99.us-east-1-4.ec2.redns.redis-cloud.com', port=14976,
    password='D18hM9eK6UYTvxCbVb3CywwbxOBFE5cU', decode_responses=True)


@db.listener(channels=['smallgo'], is_async=True)
def on_redis_message(**item):
    if item['type'] == 'message':
        msg = json.loads(item['data'])
        if msg['action'] == 'exit':
            raise StopIteration
        else:
            order_handle(msg)


def order_handle(msg):
    # 请在此处自己coding, 根据msg给交易端下单
    print(msg)


if __name__ == '__main__':
    on_redis_message()
    print('wait for message to handle')